<template>
  <div>
    <el-form ref="form">
      <el-form-item prop="search" class="mb20 mt10">
        <el-row class="row-bg">
          <el-col :xs="10" :sm="8" :md="6" :lg="4">
            <el-input
              v-model.number="search"
              size="mini"
              placeholder="输入客户号码或客户姓名搜索"
              @keyup.enter.native="getFinances(15, 0)"
            ></el-input>
          </el-col>
          <el-col :xs="6" :sm="4" :md="2" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getFinances(15, 0)"
            >搜索</el-button>
          </el-col>
          <el-col :xs="10" :sm="8" :md="6" :lg="4">
            <div class="block">
              <el-date-picker
                v-model="pickCarTime"
                @change="handlePickCarTimeSearch"
                type="month"
                placeholder="选择提车日期"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </div>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button type="success" size="mini" :icon="searchIcon" @click="advancedSearch()">高级搜索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-circle-close-outline"
              @click="resetSearch"
            >重置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <advancedSearch
      :advancedSearchs="advancedSearchs"
      v-show="showAdvancedSearch"
      @filter="handleFilter"
    ></advancedSearch>
    <financeDialog
      :title="title"
      :editDialog="editDialog"
      :financeForm="financeForm"
      @close="hanleClose"
      @update="handleUpdate"
    ></financeDialog>
    <el-table
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column type="selection" width="45"></el-table-column>
      <el-table-column prop="id" label="序号" width="65" fixed></el-table-column>
      <el-table-column sortable prop="pickCarTime" label="提车时间" width="100" fixed>
        <template slot-scope="scope">{{scope.row.pickCarTime | moment("YYYY/MM/DD")}}</template>
      </el-table-column>
      <!-- <el-table-column sortable prop="time" label="上门时间" width="95" fixed>
        <template slot-scope="scope">{{scope.row.time | moment("YYYY/MM/DD")}}</template>
      </el-table-column>-->
      <el-table-column prop="phone" label="客户电话" width="100" fixed></el-table-column>
      <el-table-column prop="name" label="客户姓名" width="70" fixed></el-table-column>
      <el-table-column prop="isMortgage" label="购车类型" width="70" fixed></el-table-column>
      <el-table-column prop="promotionName" label="推广渠道" width="70"></el-table-column>
      <el-table-column prop="deposit" label="定金(元)" width="70"></el-table-column>
      <el-table-column prop="distributionName" label="区域" width="80"></el-table-column>
      <el-table-column prop="centerName" label="中心" width="50"></el-table-column>
      <el-table-column prop="deptName" label="部门" width="70"></el-table-column>
      <el-table-column prop="salesmanName" label="业务员" width="70"></el-table-column>
      <el-table-column prop="cartype" label="购买车型" width="70"></el-table-column>
      <el-table-column prop="trial" label="审过渠道" width="80"></el-table-column>
      <el-table-column prop="charge" label="实际收费" width="80"></el-table-column>
      <el-table-column prop="cost" label="后台成本" width="80"></el-table-column>
      <el-table-column prop="achievement" label="业绩" width="70"></el-table-column>
      <el-table-column prop="operation" label="操作" width="110">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt20">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pages"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import financeDialog from "@/components/finance_dialog";
import advancedSearch from "@/components/advanced_search";
import { getFilterMonthLastDay } from "@/utils/getCurrMonth";

export default {
  name: "finance",
  data() {
    return {
      page: 15,
      total: 0,
      search: "",
      currentPage: 1,
      pages: [15, 20, 25, 35],
      showAdvancedSearch: false,
      title: "",
      tableData: [],
      val: 1,
      loading: true,
      pickCarTime: "",
      pickCarTimeSearch: ["", ""],
      depositToAchievement: "",
      advancedSearchs: ["deposit_to_achievement"],
      advanced_search: "",
      editDialog: false,
      financeForm: {},
      searchIcon: "el-icon-arrow-down"
    };
  },
  created() {
    this.getFinances(15, 0);
  },
  methods: {
    handlePickCarTimeSearch() {
      const time =
        this.pickCarTime &&
        `${this.pickCarTime.split("-")[0]}-${
          this.pickCarTime.split("-")[1]
        }-${getFilterMonthLastDay(
          this.pickCarTime.split("-")[0],
          this.pickCarTime.split("-")[1]
        )}`;
      this.pickCarTimeSearch = [];
      this.pickCarTimeSearch.push(
        this.pickCarTime ? this.pickCarTime : "",
        time
      );
      this.getFinances(15, 0);
    },
    advancedSearch() {
      this.searchIcon === "el-icon-arrow-down"
        ? (this.searchIcon = "el-icon-arrow-up")
        : (this.searchIcon = "el-icon-arrow-down");
      this.showAdvancedSearch = !this.showAdvancedSearch;
    },
    handleFilter(arg) {
      this.advanced_search = arg.search;
      this.depositToAchievement = arg.depositToAchievement;
      this.getFinances(15, 0);
    },
    handleSizeChange(val) {
      this.page = val;
      this.getFinances(val, 0);
    },
    handleCurrentChange(val) {
      this.val = val;
      // 下一页页数*每页显示行-上一页页数*每页显示行
      this.getFinances(this.page, (val - 1) * this.page);
    },
    getFinances(limit, offset) {
      this.$axios
        .get(
          `/api/finance/finances/?limit=${limit}&offset=${offset}&search=${this
            .search || this.advanced_search}&from=${
            this.pickCarTimeSearch[0]
          }&to=${this.pickCarTimeSearch[1]}&depositToAchievement=${
            this.depositToAchievement
          }`
        )
        .then(data => {
          this.tableData = data.doc;
          this.total = data.total;
          this.loading = false;
        });
    },
    resetSearch() {
      this.search = "";
      this.getFinances(15, 0);
    },
    hanleClose() {
      this.editDialog = false;
      this.resetForm();
    },
    handleEdit(item) {
      this.editDialog = !this.editDialog;
      this.title = "编辑订单";
      this.financeForm = item;
    },
    resetForm() {
      this.financeForm = {
        charge: "",
        cost: ""
      };
    },
    handleUpdate() {
      this.getFinances(this.page, (this.val - 1) * this.page);
    }
  },
  components: {
    financeDialog,
    advancedSearch
  }
};
</script>
<style>
</style>
